package youleche.com.business;

import java.sql.Connection;
import java.util.List;
import youleche.com.connection.ConnectionFactory;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.IVehicleInfo;
import youleche.com.model.MVehicleInfo;
import youleche.com.util.DateUtil;

/**
 * 车辆信息业务层
 * @author Administrator
 *
 */
public class BVehicleInfo {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 处理器
	 */
	private IVehicleInfo handler = null;
	
	/**
	 * 构造器
	 */
	public BVehicleInfo(){
		this.handler = DaoFactory.getDVehicleInfo();
	}
	
	/**
	 * 添加车辆信息
	 * @param vehicleInfo 车辆信息
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int addVehicleInfo(MVehicleInfo vehicleInfo) throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			vehicleInfo.setAddTime(DateUtil.getNowDate());
			vehicleInfo.setModifyTime(DateUtil.getNowDate());	
			vehicleInfo.setIsDelete("0");
			return this.handler.addVehicleInfo(this.con, vehicleInfo);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取首页基础加载的数据
	 * @param count 获取的条数
	 * @return 返回车辆信息
	 * @throws Exception 异常
	 */
	public List<MVehicleInfo> getVehicleInfoListForHome(
			int count) throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getVehicleInfoListForHome(this.con, count);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过ID得到车辆
	 * @param id ID
	 * @return 返回车辆信息
	 * @throws Exception 异常
	 */
	public MVehicleInfo getVehicleInfoById(String id) throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getVehicleInfoById(this.con, id);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过id获取车辆信息的访问量
	 * @param id 车辆id
	 * @return 返回访问量
	 * @throws Exception 异常
	 */
	public int getCountById(String id) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getCountById(this.con, id);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过车辆信息ID更新访问量
	 * @param id 车辆信息id
	 * @return 返回车辆信息id
	 * @throws Exception 异常
	 */
	public int updateCountById(String id, String count) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateCountById(this.con, id, count);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取品牌总数
	 * @return 返回品牌总数
	 * @throws Exception 异常
	 */
	public int getVehicleNameForCount() throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getVehicleNameForCount(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取分页数据
	 * @param startIndex 开始索引
	 * @param endIndex 结束索引
	 * @return 返回分页数据
	 * @throws Exception 异常
	 */
	public List<MVehicleInfo> getVehicleInfoList(
			String startIndex, String endIndex) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getVehicleInfoList(this.con, startIndex, endIndex);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过ID删除删除车辆信息
	 * @param id id
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int deleteVehicleInfoById( String id)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.deleteVehicleInfoById(this.con, id);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取车辆信息总数
	 * @return 车辆信息总数
	 * @throws Exception 异常
	 */
	public int getCount() throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getCount(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过商家ID得到车辆信息
	 * @param con 数据库连接
	 * @param sellerId ID
	 * @return 返回车辆信息
	 * @throws Exception 异常
	 */
	public List<MVehicleInfo> getVehicleInfoBySellerId(
			String sellerId, String startIndex, String endIndex) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getVehicleInfoBySellerId(this.con, sellerId,startIndex, endIndex);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据商户Id获取发布信息的总数量
	 * @param sellerId 商户id
	 * @return 返回发布信息的总数量
	 * @throws Exception
	 */
	public int getCountBySellerId(String sellerId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getCountBySellerId(this.con, sellerId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 搜索获取分页数据
	 * @param con 数据库连接
	 * @param startIndex 开始索引
	 * @param endIndex 结束索引
	 * @param condition 搜索条件
	 * @return 返回分页数据
	 * @throws Exception 异常
	 */
	public List<MVehicleInfo> getVehicleInfoListBySearch(
			String startIndex, String endIndex, String condition)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getVehicleInfoListBySearch(this.con, startIndex, endIndex, condition);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 更新车辆信息的首页图片地址
	 * @param imageId 图片id
	 * @param vehicleId 车辆id
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	public int updateImageIdByVehicleId(String imageId,
			String vehicleId) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.updateImageIdByVehicleId(this.con, imageId, vehicleId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
		
	/**
	 * 用户置顶车辆报价信息
	 * @param vehicleId 车辆ID
	 * @return 返回影响的行数
	 * @throws Exception
	 */
	public int topVehicleInfoById(String vehicleId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.handler.topVehicleInfoById(this.con, vehicleId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 得到该商户今天发布报价信息的条数
	 * @param sellerId 商户ID
	 * @return 返回报价信息条数
	 * @throws Exception 异常
	 */
	public String getVehicleInfoCountForTodayBySellerId(
			String sellerId) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.handler.getVehicleInfoCountForTodayBySellerId(this.con, sellerId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
}
